﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CanalDeNoticiasMSDNv5.ViewModels;
using Microsoft.Phone.Tasks;
using CanalDeNoticiasMSDNv5.lib;

namespace CanalDeNoticiasMSDNv5
{
    public partial class DetalleVideo : PhoneApplicationPage
    {
        public DetalleVideo()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (App.ViewModel != null && App.ViewModel.Videos != null && App.ViewModel.Videos.Count > 0)
            {
                refreshSource();
            }
            else
                if (App.ViewModel != null)
                {
                    App.ViewModel.Videos.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Videos_CollectionChanged);
                }
        }

        private void refreshSource()
        {
            int index = int.Parse(this.NavigationContext.QueryString["id"]);

            this.DataContext =
            App.ViewModel.Videos[index];
        }

        void Videos_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (App.ViewModel != null && App.ViewModel.Videos != null && App.ViewModel.Videos.Count > 0)
            {
                refreshSource();
                App.ViewModel.Videos.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Videos_CollectionChanged);

            }
        }
    
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        private void webBrowser_Navigating(object sender, NavigatingEventArgs e)
        {
            e.Cancel = true;

            new WebBrowserTask() { Uri = e.Uri }.Show();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            ElementoEntradaVideo video = DataContext as ElementoEntradaVideo;
            if (video != null)
            {
                new EmailComposeTask() { Subject = "MSDN Video: " + video.Title,
                                         Body = video.Link + "\n\n" + 
                                         HtmlStripper.HtmlToPlainText(video.Description)
                }
                    .Show();
            }
        }


        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            ElementoEntradaVideo video = DataContext as ElementoEntradaVideo;
            if (video != null)
            {
                new MediaPlayerLauncher() { Media=new Uri( video.Video, UriKind.Absolute) }.Show();
            }
            
        }


        private string FetchBackgroundColor()
        {
            return IsBackgroundBlack() ? "#000;" : "#fff";
        }

        private string FetchFontColor()
        {
            return IsBackgroundBlack() ? "#fff;" : "#000";
        }

        private static bool IsBackgroundBlack()
        {
            return FetchBackGroundColor() == "#FF000000";
        }

        private static string FetchBackGroundColor()
        {
            string color;
            Color mc =
              (Color)Application.Current.Resources["PhoneBackgroundColor"];
            color = mc.ToString();
            return color;
        }

        private void wb1_Loaded(object sender, RoutedEventArgs e)
        {
            string fontColor = FetchFontColor();
            string backgroundColor = FetchBackgroundColor();

            SetBackground();

            ElementoEntradaVideo video = DataContext as ElementoEntradaVideo;
            if (video != null)
            {
                string html = video.Description;// string.Format("<body style='background-color:black; color:white; font-family:\"Segoe WP\"'>{0}</body>", video.Description);
                string head = "\n<meta name='viewport' content='width=device-width,height=device-height,user-scalable=no' />\n";
                //var htmlScript = "<script>function getDocHeight() { " +
                //  "return document.getElementById('pageWrapper').offsetHeight;" +
                //  "}" +
                //  "function SendDataToPhoneApp() {" +
                //  "window.external.Notify('' + getDocHeight());" +
                //  "}</script>";

                var htmlConcat = string.Format("<html><head>{0}<style type=\"text/css\"> a:link {{color:{2} }}</style></head>" +
                  "<body style=\"margin:0px;padding:0px;background-color:{3};\" >" +
                  "<div id=\"pageWrapper\" style=\"width:100%; color:{2}; " +
                  "background-color:{3}\">{1}</div></body></html>",
                  head,
                  html,
                  fontColor,
                  backgroundColor);
                
                webBrowser.NavigateToString(htmlConcat);

            }
        }

        private void SetBackground()
        {
            Color mc =
              (Color)Application.Current.Resources["PhoneBackgroundColor"];
            webBrowser.Background = new SolidColorBrush(mc);
        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            ElementoEntradaVideo video = DataContext as ElementoEntradaVideo;

            if (video != null)
            {
                new ShareLinkTask()
                {
                    LinkUri = new Uri(video.Link),
                    Message = HtmlStripper.HtmlToPlainText(video.Description),
                    Title = video.Title
                }.Show();
            }
        }

    }
}